package com.mzj.saas.electronicVisa.fadada.contract;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasc.open.api.v5_1.req.signtask.AddFillFieldInfo;
import com.fasc.open.api.v5_1.res.template.*;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.HttpStatus;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.electronicVisa.pojo.ElectronicVisaContract;
import com.haixiaoke.saas.repository.electronicVisa.pojo.TencentContractTemplateModule;
import com.haixiaoke.saas.repository.electronicVisa.service.FadadaElectronicVisaService;
import com.haixiaoke.saas.repository.exception.BizException;
import com.haixiaoke.saas.repository.mapper.HouseMapper;
import com.haixiaoke.saas.repository.pojo.ElectronicSigningContract;
import com.haixiaoke.saas.repository.pojo.House;
import com.haixiaoke.saas.repository.pojo.OrgElectronicVisa;
import com.haixiaoke.saas.repository.service.IElectronicSigningContractService;
import com.haixiaoke.saas.repository.service.IOrgElectronicVisaService;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.commons.enums.UserLoginTypeEnum;
import com.mzj.saas.mservice.property.entity.CommunityHouseUser;
import com.mzj.saas.mservice.property.repository.CommunityHouseUserRepository;
import com.mzj.saas.mservice.property.vo.MatchUserHouseVO;
import com.mzj.saas.mservice.sys.entity.AppUser;
import com.mzj.saas.mservice.sys.repository.AppUserRepository;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 法大大电子签合同
 *
 * @author qixi
 * @date 2023/12/23
 */
@RestController
@RequestMapping("/electronicVisa/fadada/contract")
public class FadadaContractController extends BaseController {

    @Autowired
    private FadadaElectronicVisaService fadadaElectronicVisaService;
    @Autowired
    private CommunityHouseUserRepository communityHouseUserRepository;
    @Autowired
    private AppUserRepository appUserRepository;
    @Autowired
    private LoginUserUtil loginUserUtil;
    @Autowired
    private IOrgElectronicVisaService orgElectronicVisaService;
    @Autowired
    private IElectronicSigningContractService electronicSigningContractService;
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询合同模板列表
     *
     * @return
     */
    @CheckAuth
    @GetMapping("/signTempalteList")
    @ResponseBody
    public Object describeTemplates() {
        Long orgId = loginUserUtil.getOrgId();
        OrgElectronicVisa orgElectronicVisa = new OrgElectronicVisa();
        orgElectronicVisa.setOrgId(orgId);
        List<OrgElectronicVisa> electronicVisas = orgElectronicVisaService.selectOrgElectronicVisaList(orgElectronicVisa);
        if (CollectionUtil.isEmpty(electronicVisas)) {
            throw new BizException(HttpStatus.ERROR, "该企业暂无电子签服务");
        }

        try {
            List<SignTemplateListInfo> signTemplateListInfos = fadadaElectronicVisaService.signTempalteList();
            return success(signTemplateListInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return success();
    }

    /**
     * 查询合同模板控件信息
     *
     * @return
     */
    @CheckAuth
    @GetMapping("/editContractInfo/{signTemplateId}/{contractId}")
    public Object editContractInfo(@PathVariable(value = "signTemplateId") String signTemplateId,
                                   @PathVariable(value = "contractId") Long contractId) {
        try {

            Long orgId = loginUserUtil.getOrgId();
            OrgElectronicVisa orgElectronicVisa = orgElectronicVisaService.selectOrgElectronicVisaByOrgId(orgId);
            if (ObjectUtil.isNull(orgElectronicVisa)) {
                throw new BizException(HttpStatus.ERROR, "该企业暂无电子签服务");
            }

            ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);

            SignTemplateDetailRes signTemplateDetailRes = fadadaElectronicVisaService.signTempalteDetail(signTemplateId);
            List<SignTaskActorInfo> actors = signTemplateDetailRes.getActors();
            for (SignTaskActorInfo actor : actors) {
                ActorInfo actorInfo = actor.getActorInfo();
                Boolean initiator = actorInfo.getInitiator();
                if (initiator) {
                    List<AddFillFieldInfo> list = new ArrayList<>();
                    List<FillActorFieldInfo> fillFields = actor.getFillFields();
                    for (FillActorFieldInfo fillField : fillFields) {
                        AddFillFieldInfo info = new AddFillFieldInfo();
                        info.setFieldDocId(fillField.getFieldDocId().toString());
                        info.setFieldValue(buildValue(fillField.getFieldId(), electronicSigningContract, orgElectronicVisa));
                        info.setFieldId(fillField.getFieldId());
                        info.setFieldName(buildFieldName(fillField.getFieldId()));
                        list.add(info);
                    }
                    return success(list);
                }
            }
            return success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }

    private String buildFieldName(String fieldId) {
        String fieldName = "";
        switch (fieldId) {
            case TencentContractTemplateModule.HOUSE_ADDRESS:
                fieldName = "房屋地址";
                break;
            case TencentContractTemplateModule.ORG_NAME:
                fieldName = "企业名称";
                break;
            case TencentContractTemplateModule.USCC:
                fieldName = "统一社会信用代码";
                break;
            case TencentContractTemplateModule.HOUSE_TYPE:
                fieldName = "户型";
                break;
            case TencentContractTemplateModule.AREA:
                fieldName = "面积";
                break;
            case TencentContractTemplateModule.LEASE_START_TIME:
                fieldName = "租赁开始时间";
                break;
            case TencentContractTemplateModule.LEASE_END_TIME:
                fieldName = "租赁结束时间";
                break;
            case TencentContractTemplateModule.RENT:
                fieldName = "租金";
                break;
            case TencentContractTemplateModule.CASH_PLEDGE:
                fieldName = "押金";
                break;
            case TencentContractTemplateModule.PAYMENT_DATE:
                fieldName = "缴费日期";
                break;
            case TencentContractTemplateModule.DELIVERY_PERIOD:
                fieldName = "交付日期";
                break;
            case TencentContractTemplateModule.USER_NAME:
                fieldName = "承租人姓名";
                break;
            case TencentContractTemplateModule.ID_CARD:
                fieldName = "承租人身份证号";
                break;
            default:
                break;
        }
        return fieldName;
    }

    private String buildValue(String fieldId, ElectronicSigningContract electronicSigningContract, OrgElectronicVisa orgElectronicVisa) {

        House house = houseMapper.selectHouseById(electronicSigningContract.getHouseId());
        if (ObjectUtil.isEmpty(house)) {
            throw new BizException(HttpStatus.ERROR, "房屋信息异常");
        }
        String userId = electronicSigningContract.getUserId();
        Optional<AppUser> byId = appUserRepository.findById(userId);
        if (!byId.isPresent()) {
            throw new BizException(HttpStatus.ERROR, "房屋业主信息异常");
        }
        AppUser appUser = byId.get();
        String fieldValue = "";
        switch (fieldId) {
            case TencentContractTemplateModule.HOUSE_ADDRESS:
                // House house = houseRepository.findById(houseId).get();
                String communityName = house.getECommunityName();
                // 地址 address= 市 + 街道 + 小区名
                String cityName = house.getCityName();
                String address = cityName + house.getResidenceStreetName() + communityName + house.getRoomNumber();

                fieldValue = address;
                break;
            case TencentContractTemplateModule.ORG_NAME:
                fieldValue = orgElectronicVisa.getOrganizationName();
                break;
            case TencentContractTemplateModule.USCC:
                fieldValue = orgElectronicVisa.getUSCC();
                break;

            case TencentContractTemplateModule.HOUSE_TYPE:
                String houseType = "";
                Long room = house.getRoom();
                Long hall = house.getHall();
                Long toilet = house.getToilet();
                // 数字转中文
                String roomStr = Convert.numberToChinese(room, false);
                String hallStr = Convert.numberToChinese(hall, false);
                String toiletStr = Convert.numberToChinese(toilet, false);
                if (StrUtil.isNotBlank(roomStr)) {
                    houseType += roomStr + "室";
                }
                if (StrUtil.isNotBlank(hallStr)) {
                    houseType += hallStr + "厅";
                }
                if (StrUtil.isNotBlank(toiletStr)) {
                    houseType += toiletStr + "卫";
                }
                fieldValue = houseType;
                break;
            case TencentContractTemplateModule.AREA:
                fieldValue = house.getArea().toString();
                break;
            case TencentContractTemplateModule.USER_NAME:
                fieldValue = appUser.getName();
                break;
            case TencentContractTemplateModule.ID_CARD:
                fieldValue = appUser.getCardNumber();
                break;
            case TencentContractTemplateModule.LEASE_START_TIME:
                fieldValue = DateUtil.format(electronicSigningContract.getContractStartTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            case TencentContractTemplateModule.LEASE_END_TIME:
                fieldValue = DateUtil.format(electronicSigningContract.getContractEndTime(), "yyyy-MM-dd HH:mm:ss");
                break;
            // case TencentContractTemplateModule.RENT:
            //     component.setComponentValue("1000");
            //     break;
            // case TencentContractTemplateModule.CASH_PLEDGE:
            //     component.setComponentValue("1000");
            //     break;
            // case TencentContractTemplateModule.PAYMENT_DATE:
            //     component.setComponentValue("1");
            //     break;
            // case TencentContractTemplateModule.DELIVERY_PERIOD:
            //     component.setComponentValue("3");
            //     break;
            default:
                break;
        }
        return fieldValue;
    }


    /**
     * 模板流程 发起合同签署
     *
     * @return
     */
    @PostMapping("/createFlowsByTemplates")
    @ResponseBody
    @CheckAuth
    public AjaxResult createFlowsByTemplates(@RequestBody @Valid ElectronicVisaContract bean) {


        String houseId = bean.getHouseId();
        List<CommunityHouseUser> byCommunityHouseIdAndStatusAndType = communityHouseUserRepository.findByCommunityHouseIdAndStatusAndType(houseId, 1, 1);
        if (ObjectUtil.isEmpty(byCommunityHouseIdAndStatusAndType)) {
            throw new BizException(HttpStatus.ERROR, "该房屋暂无业主");
        }

        String userId = byCommunityHouseIdAndStatusAndType.get(0).getUserId();
        Optional<AppUser> byId = appUserRepository.findById(userId);
        if (!byId.isPresent()) {
            throw new BizException(HttpStatus.ERROR, "房屋业主信息异常");
        }

        AppUser appUser = byId.get();
        bean.setIdCard(appUser.getCardNumber());
        if (appUser.getCertificationStatus() == null || appUser.getCertificationStatus() != 1) {
            throw new BizException(HttpStatus.ERROR, "房屋业主信息有误");
        }
        Long contractId = bean.getContractId();
        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);
        if (ObjectUtil.isEmpty(electronicSigningContract)) {
            throw new BizException(HttpStatus.ERROR, "合同信息异常");
        }

        Long contractState = electronicSigningContract.getContractState();
        if (!contractState.equals(ElectronicSigningContract.CONTRACT_STATE_NOT)) {
            throw new BizException(HttpStatus.ERROR, "合同已生成");
        }

        Date expiresTime = bean.getExpiresTime();
        if (ObjectUtil.isNotNull(expiresTime)){
            DateTime dateTime = DateUtil.offsetMinute(new Date(), 10);
            if (expiresTime.getTime() < dateTime .getTime()){
                throw new BizException(HttpStatus.ERROR, "签署截止时间不能小于十分钟以内！");
            }
        }

        Long orgId = loginUserUtil.getOrgId();
        OrgElectronicVisa orgElectronicVisa = new OrgElectronicVisa();
        orgElectronicVisa.setOrgId(orgId);
        List<OrgElectronicVisa> electronicVisas = orgElectronicVisaService.selectOrgElectronicVisaList(orgElectronicVisa);
        if (CollectionUtil.isEmpty(electronicVisas)) {
            throw new BizException(HttpStatus.ERROR, "该企业暂无电子签服务");
        }
        OrgElectronicVisa orgElectronicVisaBean = electronicVisas.get(0);
        if (orgElectronicVisaBean.getContractUsedNumber() >= orgElectronicVisaBean.getContractTotalNumber()) {
            throw new BizException(HttpStatus.ERROR, "合同数量已用完,请联系管理员续费");
        }

        bean.setOrgElectronicVisa(orgElectronicVisaBean);
        // 发起签署
        try {
            String withTemplate = fadadaElectronicVisaService.createWithTemplate(bean);
            electronicSigningContract.setFlowId(withTemplate);
            electronicSigningContract.setContractState(ElectronicSigningContract.CONTRACT_STATE_AWAIT_SIGN);
            electronicSigningContract.setTemplateId(bean.getTemplateId());
            electronicSigningContract.setAgentId(orgElectronicVisaBean.getProxyOperatorOpenid());
            electronicSigningContract.setContractCreateTime(new Date());
            electronicSigningContractService.updateElectronicSigningContract(electronicSigningContract);


            orgElectronicVisaBean.setContractUsedNumber(orgElectronicVisaBean.getContractUsedNumber() + 1);
            orgElectronicVisaService.updateOrgElectronicVisa(orgElectronicVisa);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return success();
    }

    /**
     * 查询电子签合同信息列表
     */
    @GetMapping("/list")
    @CheckAuth
    public TableDataInfo list(ElectronicSigningContract electronicSigningContract) {
        startPage();
        Integer loginType = loginUserUtil.getLoginType();
        List<ElectronicSigningContract> list;
        if (loginType.equals(1)) {
            Long id = loginUserUtil.getOrgId();
            electronicSigningContract.setOrgId(id);
            list = electronicSigningContractService.selectElectronicSigningContractOrHouseInfoList(electronicSigningContract);
        } else {
            String id = loginUserUtil.getId();
            electronicSigningContract.setUserId(id);
            list = electronicSigningContractService.selectElectronicSigningContractOrHouseInfoListByUser(electronicSigningContract);
        }
        return getDataTable(list);
    }

    /**
     * 查询企业电子签合同信息列表
     */
    @GetMapping("/contractList")
    @CheckAuth
    public TableDataInfo contractList(ElectronicSigningContract electronicSigningContract) {
        startPage();
        Long id = loginUserUtil.getOrgId();
        electronicSigningContract.setOrgId(id);
        List<ElectronicSigningContract> list = electronicSigningContractService.selectElectronicSigningContractOrHouseInfoContractList(electronicSigningContract);
        return getDataTable(list);
    }

    // /**
    //  * 导出电子签合同信息列表
    //  */
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, ElectronicSigningContract electronicSigningContract) {
    //     List<ElectronicSigningContract> list = electronicSigningContractService.selectElectronicSigningContractList(electronicSigningContract);
    //     ExcelUtil<ElectronicSigningContract> util = new ExcelUtil<ElectronicSigningContract>(ElectronicSigningContract.class);
    //     util.exportExcel(response, list, "电子签合同信息数据");
    // }

    /**
     * 获取电子签合同信息详细信息
     */
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId) {
        return success(electronicSigningContractService.selectElectronicSigningContractByContractId(contractId));
    }

    /**
     * 新增电子签合同信息
     */
    @PostMapping
    @CheckAuth
    public AjaxResult add(@RequestBody @Valid ElectronicSigningContract electronicSigningContract) {
        Long orgId = loginUserUtil.getOrgId();
        String orgName = loginUserUtil.getOrgName();

        OrgElectronicVisa orgElectronicVisa = orgElectronicVisaService.selectOrgElectronicVisaByOrgId(orgId);
        if (orgElectronicVisa.getContractUsedNumber() >= orgElectronicVisa.getContractTotalNumber()) {
            throw new BizException(HttpStatus.ERROR, "合同数量已用完,请联系管理员");
        }


        String userPhone = electronicSigningContract.getUserPhone();
        AppUser appUser = appUserRepository.findByPhoneAndStatusAndType(userPhone, 1, UserLoginTypeEnum.PROGRAM.getValue());

        if (ObjectUtil.isEmpty(appUser) || appUser.getCertificationStatus() != 1) {
            throw new BizException(HttpStatus.ERROR, "该手机号未注册或未实名认证");
        }

        electronicSigningContract.setOrgId(orgId);
        electronicSigningContract.setOrgName(orgName);
        electronicSigningContract.setUserId(appUser.getId());
        electronicSigningContract.setUserName(appUser.getName());


        String queryIsLessee = "SELECT muh.* FROM m_match_user_house muh WHERE muh.house_id = ? ";
        List<MatchUserHouseVO> query = jdbcTemplate.query(queryIsLessee, new BeanPropertyRowMapper<>(MatchUserHouseVO.class), electronicSigningContract.getHouseId());
        MatchUserHouseVO matchUserHouseVO = query.get(0);
        electronicSigningContract.setContractStartTime(matchUserHouseVO.getStartDate());
        electronicSigningContract.setContractEndTime(matchUserHouseVO.getEndDate());

        if (electronicSigningContractService.checkContractExist(electronicSigningContract)) {
            throw new BizException(HttpStatus.ERROR, "该房屋已生成合同,不可重复生成");
        }
        electronicSigningContractService.insertElectronicSigningContract(electronicSigningContract);
        return success(electronicSigningContract);
    }

    /**
     * 修改电子签合同信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ElectronicSigningContract electronicSigningContract) {
        return toAjax(electronicSigningContractService.updateElectronicSigningContract(electronicSigningContract));
    }

    /**
     * 删除电子签合同信息
     */
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(electronicSigningContractService.deleteElectronicSigningContractByContractIds(contractIds));
    }


    /**
     * 获取模板预览链接
     *
     * @param templateId
     * @return
     */
    @GetMapping("/getTemplatePreviewUrl/{templateId}")
    @CheckAuth
    public AjaxResult previewContract(@PathVariable String templateId) {
        Long orgId = loginUserUtil.getOrgId();
        OrgElectronicVisa orgElectronicVisa = orgElectronicVisaService.selectOrgElectronicVisaByOrgId(orgId);
        try {
            String templatePreviewUrl = fadadaElectronicVisaService.getTemplatePreviewUrl(templateId, orgElectronicVisa);
            return AjaxResult.success("success", templatePreviewUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }

    /**
     * 获取合同签署预览链接
     *
     * @param contractId
     * @return
     */
    @GetMapping("/getSignTaskPreviewUrl/{contractId}")
    @CheckAuth
    public AjaxResult getSignTaskPreviewUrl(@PathVariable Long contractId) {
        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);
        try {
            String templatePreviewUrl = fadadaElectronicVisaService.getSignTaskPreviewUrl(electronicSigningContract.getFlowId());
            return AjaxResult.success("success", templatePreviewUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }

    /**
     * 获取合同下载地址
     *
     * @param
     * @return
     */
    @GetMapping("/getOwnerDownloadUrl/{contractId}")
    @CheckAuth
    public AjaxResult getOwnerDownloadUrl(@PathVariable Long contractId) {

        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);
        try {
            if (ObjectUtil.isEmpty(electronicSigningContract)) {
                throw new BizException(HttpStatus.ERROR, "合同未查询到");
            }
            Long orgId = loginUserUtil.getOrgId();
            OrgElectronicVisa orgElectronicVisa = orgElectronicVisaService.selectOrgElectronicVisaByOrgId(orgId);
            String ownerDownloadUrl = fadadaElectronicVisaService.getOwnerDownloadUrl(electronicSigningContract, orgElectronicVisa.getOpenId());
            return AjaxResult.success("success", ownerDownloadUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }


    /**
     * 完成合同
     *
     * @param contractId
     * @return
     */
    @GetMapping("/completeContract/{contractId}")
    @CheckAuth
    public AjaxResult completeContract(@PathVariable Long contractId) {
        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);
        try {
            if (ObjectUtil.isEmpty(electronicSigningContract)) {
                throw new BizException(HttpStatus.ERROR, "合同未生成");
            }

            if (electronicSigningContract.getContractState() != 2) {
                throw new BizException(HttpStatus.ERROR, "合同签署未完成");
            }

            electronicSigningContract.setContractState(ElectronicSigningContract.CONTRACT_STATE_SIGN_OK);
            electronicSigningContract.setUpdateTime(DateUtil.date());
            electronicSigningContractService.updateElectronicSigningContract(electronicSigningContract);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }

    /**
     * 获取合同签署链接
     *
     * @param contractId
     * @return
     */
    @GetMapping("/getActorUrl/{contractId}")
    @CheckAuth
    public AjaxResult getActorUrl(@PathVariable Long contractId) {
        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);

        if (ObjectUtil.isEmpty(electronicSigningContract)) {
            throw new BizException(HttpStatus.ERROR, "合同未生成");
        }

        // if (electronicSigningContract.getContractState() != ElectronicSigningContract.CONTRACT_STATE_TENANT_SIGN) {
        //     throw new BizException(HttpStatus.ERROR, "合同状态有误无法签署");
        // }

        Integer loginType = loginUserUtil.getLoginType();
        try {
            String templatePreviewUrl = fadadaElectronicVisaService.getActorUrl(electronicSigningContract, loginType);
            return AjaxResult.success("success", templatePreviewUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success();
    }

    /**
     * 合同撤销
     *
     * @param contractId
     * @return
     */
    @GetMapping("/cancel/{contractId}")
    @CheckAuth
    public AjaxResult cancel(@PathVariable Long contractId) {
        ElectronicSigningContract electronicSigningContract = electronicSigningContractService.selectElectronicSigningContractByContractId(contractId);
        if (ObjectUtil.isEmpty(electronicSigningContract)) {
            throw new BizException(HttpStatus.ERROR, "合同未生成");
        }
        Long contractState = electronicSigningContract.getContractState();
        if (contractState.equals(ElectronicSigningContract.CONTRACT_STATE_SIGN_OK)
                || contractState.equals(ElectronicSigningContract.CONTRACT_STATE_STOP)
                || contractState.equals(ElectronicSigningContract.CONTRACT_STATE_OVERDUE
        )) {
            // bug 2422
            throw new BizException(HttpStatus.ERROR, "合同状态异常，请刷新后再试");
        }
        try {
            // 已生成的合同需要回调变更状态 未生成的直接手动变更
            if (contractState.equals(ElectronicSigningContract.CONTRACT_STATE_NOT)) {
                electronicSigningContract.setContractState(ElectronicSigningContract.CONTRACT_STATE_STOP);
                electronicSigningContract.setUpdateTime(DateUtil.date());
                electronicSigningContract.setUpdateBy(loginUserUtil.getId());
                electronicSigningContractService.updateElectronicSigningContract(electronicSigningContract);
            } else {
                fadadaElectronicVisaService.cancel(electronicSigningContract);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }

    }


}
